/*
 * acm1141.c
 *
 *  Created on: 4 Apr 2010
 *      Author: xuxu
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int n;
	int nodefather[2000];
	int nodecount[2000];
	int con,s_count;
	int f,son;
	int node1,node2,node1tmp,node2tmp;
	int i,j;
	int bo;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<=n;i++)
			nodefather[i]=-1;
		memset(nodecount,0,sizeof(nodecount));
		con=n;
		while(con--)
		{
			scanf("%d:(%d)",&f,&s_count);
			for(i=0;i<s_count;i++)
			{
				scanf("%d",&son);
				nodefather[son]=f;
			}
		}
		scanf("%d",&con);
		while(con--)
		{
			scanf("%*c%*c%d%*c%d%*c)",&node1,&node2);
			node1tmp=node1;
			node2tmp=node2;
			bo=0;
			while(node1tmp!=-1)
			{
				node2tmp=node2;
				while(node2tmp!=-1)
				{
					if(node1tmp==node2tmp)
					{
						nodecount[node1tmp]++;
						bo=1;
						break;
					}
					node2tmp=nodefather[node2tmp];
				}
				if(bo)
					break;
				node1tmp=nodefather[node1tmp];
			}
		}
		for(i=1;i<=n;i++)
		{
			if(nodecount[i])
				printf("%d:%d\n",i,nodecount[i]);
		}
	}
	return 0;
}
